﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PasswordForm.cs" company="">
//   
// </copyright>
// <summary>
//   Formulaire Windows permettant de demander le mot de
//   passe d'un utilisateur
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ComparisonTool.Forms.Connection
{
    using System;
    using System.Windows.Forms;

    /// <summary>
    /// The password form.
    /// </summary>
    public partial class PasswordForm : Form
    {
        #region Variables

        /// <summary>
        /// The user login.
        /// </summary>
        private string userLogin;

        /// <summary>
        /// The user domain.
        /// </summary>
        private string userDomain;

        /// <summary>
        /// The user password.
        /// </summary>
        private string userPassword;

        #endregion

        #region Constructeur

        /// <summary>
        /// Initializes a new instance of the <see cref="PasswordForm"/> class.
        /// </summary>
        public PasswordForm()
        {
            this.InitializeComponent();
        }

        #endregion

        #region Propriétés

        /// <summary>
        /// Gets or sets the user login.
        /// </summary>
        public string UserLogin
        {
            get { return this.userLogin; }
            set { this.userLogin = value; }
        }

        /// <summary>
        /// Gets or sets the user domain.
        /// </summary>
        public string UserDomain
        {
            get { return this.userDomain; }
            set { this.userDomain = value; }
        }

        /// <summary>
        /// Gets the user password.
        /// </summary>
        public string UserPassword
        {
            get { return this.userPassword; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether save password.
        /// </summary>
        public bool SavePassword { get; set; }

        #endregion

        #region Méthodes

        /// <summary>
        /// The on load.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnLoad(EventArgs e)
        {
            this.tbUserLogin.Text = string.Format(
                "{0}{1}{2}", 
                this.userDomain, 
                this.userDomain.Length > 0 ? "\\" : string.Empty, 
                this.userLogin);

            base.OnLoad(e);
        }

        /// <summary>
        /// The validate click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ValidateClick(object sender, EventArgs e)
        {
            var go = true;

            if (this.tbPassword.Text.Length == 0)
            {
                if (MessageBox.Show(this, @"Are you sure you want to leave the password empty?", @"Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    go = false;
                }
            }

            if (go)
            {
                this.userPassword = this.tbPassword.Text;
                this.SavePassword = this.chkSavePassword.Checked;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        /// <summary>
        /// The cancel click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void CancelClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// The tab password key press.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void TabPasswordKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.ValidateClick(null, null);
            }
        }

        /// <summary>
        /// The chk show characters_ checked changed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ChkShowCharactersCheckedChanged(object sender, EventArgs e)
        {
            this.tbPassword.PasswordChar = this.chkShowCharacters.Checked ? (char)0 : '•';
        }

        #endregion
    }
}
